/** @file 
* Configuration par fichier*/

#include <error.h>
#include <errno.h>
#include <string.h>
#include "returns.h"
#include "graph/xmlparser.h"

#include "config.h"

// Variable a exporter
CFG_Visu config;

void CFG_checkProp(xmlNodePtr node, char* name, char* prop)
{
  unsigned char* _t = NULL;
  if(!(_t = xmlGetProp(node, BAD_CAST prop)))
  {
    error_at_line(RV_CONFIG_FAILED,0,__FILE__,__LINE__,
                  "Can't find %s.%s in config file !\n",
                  name,prop);
  } 

  xmlFree(_t);

  return;
}
void CFG_loadConfig(char* filename)
{
  xmlDocPtr doc;
  xmlNodePtr root,cur;

  // Inits 
  memset(config.network_proxy,0,CONFIGURATION_MAX_LENGTH);

  if( !(doc = xmlParseFile(filename) ) )
  {
    error_at_line(RV_CONFIG_FAILED, 0, __FILE__, __LINE__,
                    "Configuration not parsed successfully.\n");
    xmlFreeDoc(doc);
    xmlCleanupParser();
    return;
  }

  if( !(root = xmlDocGetRootElement(doc)) )
  {
    error_at_line(RV_CONFIG_FAILED, 0, __FILE__, __LINE__,
                    "Configuration has no root element.\n");
    xmlFreeDoc(doc);
    xmlCleanupParser();
    return;
  }

  for( cur = root->children; cur != NULL; cur = cur->next )
  {
    if( xmlStrEqual(cur->name, BAD_CAST "screen") )
    {
      CFG_checkProp(cur,"screen","w");
      CFG_checkProp(cur,"screen","h");
      CFG_checkProp(cur,"screen","fps");
      CFG_checkProp(cur,"screen","fullscreen");

      config.screen_w = xmlGetIntProp(cur, BAD_CAST "w");
      config.screen_h = xmlGetIntProp(cur, BAD_CAST "h");
      config.screen_fps = xmlGetFloatProp(cur, BAD_CAST "fps");
      config.screen_fullscreen = 
          xmlGetIntProp(cur, BAD_CAST"fullscreen");
    }

    if( xmlStrEqual(cur->name, BAD_CAST "network") )
    {
      CFG_checkProp(cur,"network","proxy");
      
      config.network_useproxy = xmlGetIntProp(cur, BAD_CAST"use_proxy");

      char* s = (char*)xmlGetProp(cur, BAD_CAST"proxy");
      if(s)
      {
        if( strlen(s) > CONFIGURATION_MAX_LENGTH )
          error_at_line(RV_CONFIG_FAILED, 0, __FILE__, __LINE__,
                    "Proxy URL is too long, please adjust CONFIGURATION_MAX_LENGTH in source code.");
            
        strncpy(config.network_proxy,s,CONFIGURATION_MAX_LENGTH);
        xmlFree(s);
      }

    }

    if( xmlStrEqual(cur->name, BAD_CAST "cache") )
    {
      CFG_checkProp(cur,"cache","path");
      CFG_checkProp(cur,"cache","file");

      char* s = (char*)xmlGetProp(cur, BAD_CAST"path");
      if(s)
      {
        if( strlen(s) > CONFIGURATION_MAX_LENGTH )
          error_at_line(RV_CONFIG_FAILED, 0, __FILE__, __LINE__,
                    "Cache path is too long, please adjust CONFIGURATION_MAX_LENGTH in source code.");
            
        strncpy(config.cache_path,s,CONFIGURATION_MAX_LENGTH);
        xmlFree(s);
      }

      s = (char*)xmlGetProp(cur, BAD_CAST"file");
      if(s)
      {
        if( strlen(s) > CONFIGURATION_MAX_LENGTH )
          error_at_line(RV_CONFIG_FAILED, 0, __FILE__, __LINE__,
                    "Cache filename is too long, please adjust CONFIGURATION_MAX_LENGTH in source code.");
            
        strncpy(config.cache_file,s,CONFIGURATION_MAX_LENGTH);
        xmlFree(s);
      }
    }


    if( xmlStrEqual(cur->name, BAD_CAST "grid") )
    {
      CFG_checkProp(cur,"grid","n");
      CFG_checkProp(cur,"grid","xspacing");
      CFG_checkProp(cur,"grid","yspacing");
      CFG_checkProp(cur,"grid","groundzscale");

      config.grid_n = xmlGetIntProp(cur, BAD_CAST "n");
      config.grid_xspacing = xmlGetFloatProp(cur, BAD_CAST "xspacing");
      config.grid_yspacing = xmlGetFloatProp(cur, BAD_CAST"yspacing");
      config.grid_groundzscale = xmlGetFloatProp(cur, BAD_CAST"groundzscale");
    }

    if( xmlStrEqual(cur->name, BAD_CAST "camera") )
    {
      CFG_checkProp(cur,"camera","angle");
      CFG_checkProp(cur,"camera","distance");
      CFG_checkProp(cur,"camera","height");
      CFG_checkProp(cur,"camera","fov");
      CFG_checkProp(cur,"camera","znear");
      CFG_checkProp(cur,"camera","zfar");
      CFG_checkProp(cur,"camera","vrot");

      config.camera_angle = xmlGetFloatProp(cur, BAD_CAST"angle");
      config.camera_distance = xmlGetFloatProp(cur, BAD_CAST"distance");
      config.camera_height = xmlGetFloatProp(cur, BAD_CAST"height");
      config.camera_fov = xmlGetFloatProp(cur, BAD_CAST"fov");
      config.camera_znear = xmlGetFloatProp(cur, BAD_CAST"znear");
      config.camera_zfar = xmlGetFloatProp(cur, BAD_CAST"zfar");
      config.camera_vrot = xmlGetFloatProp(cur, BAD_CAST"vrot");
    }

    if( xmlStrEqual(cur->name, BAD_CAST "texture") )
    {
      CFG_checkProp(cur,"texture","size");

      config.texture_size = xmlGetIntProp(cur, BAD_CAST"size");
    } 

    if( xmlStrEqual(cur->name, BAD_CAST "clients") )
    {
      CFG_checkProp(cur,"clients","drag_length");
      CFG_checkProp(cur,"clients","drag_width");
      CFG_checkProp(cur,"clients","circle_width");
      CFG_checkProp(cur,"clients","circle_size");
      CFG_checkProp(cur,"clients","color_rc");
      CFG_checkProp(cur,"clients","color_rm");
      CFG_checkProp(cur,"clients","color_gc");
      CFG_checkProp(cur,"clients","color_gm");
      CFG_checkProp(cur,"clients","color_bc");
      CFG_checkProp(cur,"clients","color_bm");
      CFG_checkProp(cur,"clients","zoffset");

      config.clients_dragLength =
          xmlGetIntProp(cur, BAD_CAST"drag_length");

      config.clients_dragWidth =
          xmlGetIntProp(cur, BAD_CAST"drag_width");

      config.clients_circleSize =
          xmlGetIntProp(cur, BAD_CAST"circle_size");

      config.clients_circleWidth =
          xmlGetIntProp(cur, BAD_CAST"circle_width");

       config.clients_colorRC =
          xmlGetFloatProp(cur, BAD_CAST"color_rc");

      config.clients_colorGC =
          xmlGetFloatProp(cur, BAD_CAST"color_gc");

      config.clients_colorBC =
          xmlGetFloatProp(cur, BAD_CAST"color_bc");
     
      config.clients_colorRM =
          xmlGetFloatProp(cur, BAD_CAST"color_rm");

      config.clients_colorGM =
          xmlGetFloatProp(cur, BAD_CAST"color_gm");

      config.clients_colorBM =
          xmlGetFloatProp(cur, BAD_CAST"color_bm");

      config.clients_zoffset =
          xmlGetFloatProp(cur, BAD_CAST"zoffset");
    } 

    if( xmlStrEqual(cur->name, BAD_CAST "roads") )
    {
      CFG_checkProp(cur,"roads","zoffset");

      config.roads_zoffset =
          xmlGetFloatProp(cur, BAD_CAST"zoffset");
    }
  }


  xmlFreeDoc(doc);
  xmlCleanupParser();

  return;
}
